//
//  DefenceMapLayer.cpp
//  airfight
//
//  Created by lahm on 12-12-7.
//  Copyright (c) 2012年 moonriver. All rights reserved.
//

#include "DefenceMapLayer.h"
#include "MenuLayer.h"
#include "MsgBoxLayer.h"
#include "BaseContructionLayer.h"

bool DefenceMapLayer::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    m_SpriteFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
    m_SpriteFrameCache->addSpriteFramesWithFile("airfight.plist","airfight.png");
    m_SpriteBatch = CCSpriteBatchNode::create("airfight.png");
    
    CCSprite *back = CCSprite::create();
    back->initWithSpriteFrameName("base.png");
    back->setPosition(ccp(160, 240));
    addChild(back);
    
    CCLabelTTF *labelTitle = CCLabelTTF::labelWithString("防御地图", CCSize(150, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "", 30);
    labelTitle->setPosition(ccp(160,380));
    addChild(labelTitle);
    
    m_pLabel1 = CCLabelTTF::labelWithString("火核攻击机 Lv1", CCSize(120, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "", 15);
    m_pLabel1->setPosition(ccp(90, 250));
    addChild(m_pLabel1);
    
    m_pLabel2 = CCLabelTTF::labelWithString("飓风轰炸机 Lv2", CCSize(120, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "", 15);
    m_pLabel2->setPosition(ccp(90, 200));
    addChild(m_pLabel2);
    
    CCLabelTTF *label3 = CCLabelTTF::labelWithString("地图名称：宇宙深处", CCSize(320, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "", 15);
    label3->setPosition(ccp(160, 100));
    addChild(label3);
    
    CCLabelTTF *label4 = CCLabelTTF::labelWithString("地图等级：Lv1", CCSize(320, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "", 15);
    label4->setPosition(ccp(160, 80));
    addChild(label4);
    
    CCLabelTTF *label5 = CCLabelTTF::labelWithString("地图防御：战机2 炮台1", CCSize(320, 50), kCCTextAlignmentCenter, kCCVerticalTextAlignmentCenter, "", 15);
    label5->setPosition(ccp(160, 60));
    addChild(label5);
    
    CCSprite *contructionUpgradeBtn = CCSprite::create();
    contructionUpgradeBtn->initWithSpriteFrameName("button.png");
    CCSprite *contructionUpgradeBtnClicked = CCSprite::create();
    contructionUpgradeBtnClicked->initWithSpriteFrameName("button.png");
    contructionUpgradeBtnClicked->setScale(0.95);
    CCMenuItemSprite *constructionUpgradeMenuItem = CCMenuItemSprite::itemWithNormalSprite(contructionUpgradeBtn, contructionUpgradeBtnClicked, contructionUpgradeBtn, this, menu_selector(DefenceMapLayer::clickContructionUpgrade));
    constructionUpgradeMenuItem->setPosition(ccp(100, 320));
    CCLabelTTF *labelConstructionUpgrade = CCLabelTTF::labelWithString("建筑升级", CCSize(70, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelConstructionUpgrade->setPosition(ccp(100, 322));
    
    CCSprite *defenceUpgradeBtn = CCSprite::create();
    defenceUpgradeBtn->initWithSpriteFrameName("button.png");
    CCSprite *defenceUpgradeBtnClicked = CCSprite::create();
    defenceUpgradeBtnClicked->initWithSpriteFrameName("button.png");
    defenceUpgradeBtnClicked->setScale(0.95);
    CCMenuItemSprite *defenceUpgradeMenuItem = CCMenuItemSprite::itemWithNormalSprite(defenceUpgradeBtn, defenceUpgradeBtnClicked, defenceUpgradeBtn, this, menu_selector(BaseContructionLayer::clickDefenceUpgrade));
    defenceUpgradeMenuItem->setPosition(ccp(200, 320));
    CCLabelTTF *labelDefenceUpgrade = CCLabelTTF::labelWithString("防御升级", CCSize(70, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelDefenceUpgrade->setPosition(ccp(200, 322));
    
    CCSprite *upgrade1Btn = CCSprite::create();
    upgrade1Btn->initWithSpriteFrameName("button.png");
    CCSprite *upgrade1BtnClicked = CCSprite::create();
    upgrade1BtnClicked->initWithSpriteFrameName("button.png");
    upgrade1BtnClicked->setScale(0.95);
    CCMenuItemSprite *upgrade1MenuItem = CCMenuItemSprite::itemWithNormalSprite(upgrade1Btn, upgrade1BtnClicked, upgrade1Btn, this, menu_selector(DefenceMapLayer::clickUpgrade1));
    upgrade1MenuItem->setPosition(ccp(220, 250));
    CCLabelTTF *labelUpgrade1 = CCLabelTTF::labelWithString("升级", CCSize(50, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelUpgrade1->setPosition(ccp(230, 252));
    
    CCSprite *upgrade2Btn = CCSprite::create();
    upgrade2Btn->initWithSpriteFrameName("button.png");
    CCMenuItemSprite *upgrade2MenuItem = CCMenuItemSprite::itemWithNormalSprite(upgrade2Btn, upgrade2Btn, upgrade2Btn, this, menu_selector(DefenceMapLayer::clickUpgrade2));
    upgrade2MenuItem->setPosition(ccp(220, 200));
    CCLabelTTF *labelUpgrade2 = CCLabelTTF::labelWithString("升级", CCSize(50, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelUpgrade2->setPosition(ccp(230, 202));
    
    CCSprite *backBtn = CCSprite::create();
    backBtn->initWithSpriteFrameName("button.png");
    CCSprite *backBtnClicked = CCSprite::create();
    backBtnClicked->initWithSpriteFrameName("button.png");
    backBtnClicked->setScale(0.95);
    CCMenuItemSprite *backMenuItem = CCMenuItemSprite::itemWithNormalSprite(backBtn, backBtnClicked, backBtn, this, menu_selector(BaseContructionLayer::clickBack));
    backMenuItem->setPosition(ccp(220, 150));
    CCLabelTTF *labelBack = CCLabelTTF::labelWithString("返回", CCSize(50, 30), kCCTextAlignmentLeft, kCCVerticalTextAlignmentCenter, "", 15);
    labelBack->setPosition(ccp(230, 152));
  
    CCMenu *menu = CCMenu::menuWithItems(constructionUpgradeMenuItem, defenceUpgradeMenuItem, upgrade1MenuItem, upgrade2MenuItem, backMenuItem,NULL);
    menu->setPosition(CCPointZero);
    addChild(menu);
    addChild(labelConstructionUpgrade);
    addChild(labelDefenceUpgrade);
    addChild(labelUpgrade1);
    addChild(labelUpgrade2);
    addChild(labelBack);

    return true;
}

void DefenceMapLayer::startLoading()
{
    
}

void DefenceMapLayer::onEnter()
{
    CCLayer::onEnter();
    scheduleOnce(schedule_selector(DefenceMapLayer::startLoading), 0);
}

void DefenceMapLayer::clickBack()
{
    CCScene *pScene = CCScene::create();
    CCLayer *pLayer = MenuLayer::create();
    pScene->addChild(pLayer);
    CCDirector::sharedDirector()->replaceScene(pScene);
}

void DefenceMapLayer::clickContructionUpgrade()
{
    CCScene *pScene = CCScene::create();
    CCLayer *pLayer = BaseContructionLayer::create();
    pScene->addChild(pLayer);
    CCDirector::sharedDirector()->replaceScene(pScene);
}

void DefenceMapLayer::clickDefenceUpgrade()
{
    
}

void DefenceMapLayer::clickUpgrade1()
{
    MsgBoxLayer *msgBox = MsgBoxLayer::node();
    msgBox->initDialog("确定消耗50金币，100矿石，将火核攻击机升级到Lv2？", menu_selector(DefenceMapLayer::clickOk1), NULL);
    addChild(msgBox);
}

void DefenceMapLayer::clickOk1(CCObject *pSender)
{
    ((DefenceMapLayer*)getParent())->m_pLabel1->setString("火核攻击机 Lv2");
    removeFromParentAndCleanup(true);
}

void DefenceMapLayer::clickUpgrade2()
{
    MsgBoxLayer *msgBox = MsgBoxLayer::node();
    msgBox->initDialog("确定消耗70金币，150矿石，将飓风轰炸机升级到Lv3？", menu_selector(DefenceMapLayer::clickOk2), NULL);
    addChild(msgBox);
}

void DefenceMapLayer::clickOk2(CCObject *pSender)
{
    ((DefenceMapLayer*)getParent())->m_pLabel2->setString("飓风轰炸机 Lv3");
    removeFromParentAndCleanup(true);
}
